﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Extensil.Common
{
    public partial class AlertBox :Form
    {
        private AlertBox(AlertType type, string message, string caption, AlertBoxButtons buttons)
        {
            InitializeComponent();
            rtbMessage.Text = message;
            this.Text = caption;
            btnOK.Visible = false;
            btnCancel.Visible = false;
            btnYes.Visible = false;
            btnNo.Visible = false;
            switch(buttons)
            {
                case AlertBoxButtons.OK:
                    btnOK.Visible = true;
                    break;
                case AlertBoxButtons.OKCancel:
                    btnOK.Visible = true;
                    btnCancel.Visible = true;
                    break;
                case AlertBoxButtons.YesNo:
                    btnYes.Visible = true;
                    btnNo.Visible = true;
                    break;
            }
            pictureBox1.Image = type == AlertType.Error 
                ? imageList1.Images["error.JPG"] 
                : imageList1.Images["info.JPG"];
            try
            {
                Console.WriteLine(DateTime.Now.ToLongTimeString() + "\t" + caption + ": " + message);
            }
            catch(Exception exc)
            {
            }
        }
        public static AlertResult ShowDialog(string message, Exception exc)
        {
            return ShowDialog(new Exception(message + LinkedException(exc)));
        }
        public static AlertResult ShowDialog(Exception exc)
        {
            return ShowDialog(LinkedException(exc), AlertBoxButtons.OK, AlertType.Error);
        }
        static string LinkedException(Exception exc)
        {
            return exc.Message
                + (String.IsNullOrEmpty(exc.StackTrace) ? "" : "\r\nStack Trace: " + exc.StackTrace + "]")
                + (exc.InnerException == null ? "" : "\r\n\r\nInner Exception: " + LinkedException(exc.InnerException));
        }
        public static AlertResult ShowDialog(string message)
        {
            return ShowDialog(message, AlertBoxButtons.OK);
        }
        public static AlertResult ShowDialog(string message, AlertBoxButtons buttons)
        {
            return ShowDialog(message, buttons, AlertType.Info); 
        }
        public static AlertResult ShowDialog(string message, AlertBoxButtons buttons, AlertType type)
        {
            return ShowDialog(message, "Alert", buttons, type);
        }
        public static AlertResult ShowDialog(string message, string caption, AlertBoxButtons buttons)
        {
            return ShowDialog(message, "Alert", buttons, AlertType.Info);
        }
        public static AlertResult ShowDialog(string message, string caption, AlertBoxButtons buttons, AlertType type)
        {
            AlertBox aBox = new AlertBox(type, message, caption, buttons);
            switch(aBox.ShowDialog())
            {
                case DialogResult.OK:
                    return AlertResult.OK;
                case DialogResult.Cancel:
                    return AlertResult.Cancel;
                case DialogResult.Yes:
                    return AlertResult.Yes;
                case DialogResult.No:
                    return AlertResult.No;
                default:
                    return AlertResult.Cancel;
            }
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            Close(DialogResult.OK);
        }
        void Close(DialogResult dResult)
        {
            this.DialogResult = dResult;
            this.Close();
        }

        private void btnNo_Click(object sender, EventArgs e)
        {
            Close(DialogResult.No);
        }

        private void btnYes_Click(object sender, EventArgs e)
        {
            Close(DialogResult.Yes);
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close(DialogResult.Cancel);
        }
    }
    public enum AlertType
    {
        Error,
        Info
    }
    public enum AlertBoxButtons
    {
        OK,
        OKCancel,
        YesNo
    }
    public enum AlertResult
    {
        OK,
        Cancel,
        Yes,
        No
    }
}
